package com.arpa.ntocc.common.service.authorize;

import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CreateCache;
import com.arpa.core.utils.number.RandomUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * 短信验证码
 */

@Component
public class VerificationCodeService {

    @CreateCache(name = "verification_code", expire = 1800)
    private Cache<String, String> verificationCodeCache;

    /**
     * @param phone
     * @Description: 获取登录验证码
     * @Return: java.lang.String
     * @Author: LEO
     * @Date: 2018/11/7 16:25
     */
    public String getLoginCode(String phone) {
        String verificationCode = RandomUtil.randomNumberStringFixLength(6);
        verificationCodeCache.put(phone + "_login", verificationCode);
        return verificationCode;
    }

    /**
     * @param phone
     * @Description: 获取重置密码验证码
     * @Return: java.lang.String
     * @Author: LEO
     * @Date: 2018/11/7 16:25
     */
    public String getResetCode(String phone) {
        String verificationCode = RandomUtil.randomNumberStringFixLength(6);
        verificationCodeCache.put(phone + "_reset", verificationCode);
        return verificationCode;
    }

    /**
     * @param phone
     * @Description: 获取注册验证码
     * @Return: java.lang.String
     * @Author: LEO
     * @Date: 2018/11/7 16:31
     */
    public String getRegisterCode(String phone) {
        String verificationCode = RandomUtil.randomNumberStringFixLength(6);
        verificationCodeCache.put(phone + "_register", verificationCode);
        return verificationCode;
    }

    public boolean checkRegisterCode(String phone, String verificationCode) {
        String code = verificationCodeCache.get(phone + "_register");
        return StringUtils.equals(code, verificationCode);
    }

    public boolean checkResetCode(String phone, String verificationCode) {
        String code = verificationCodeCache.get(phone + "_reset");
        return StringUtils.equals(code, verificationCode);
    }

    public boolean checkLoginCode(String phone, String verificationCode) {
        String code = verificationCodeCache.get(phone + "_login");
        return StringUtils.equals(code, verificationCode);
    }
}
